package com.inspur.cloud.service.dataspace;

import com.inspur.cloud.dao.dataspace.UserResourceRelationDao;
import com.inspur.cloud.entity.dataspace.UserResourceRelationEntity;
import com.inspur.cloud.entity.dataspace.vo.auth.AuthInfoVO;
import com.inspur.cloud.entity.dataspace.vo.auth.AuthUpdateVO;
import com.inspur.cloud.entity.dataspace.vo.auth.PowerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PowerService {

    @Autowired
    private AuthService authService;
    @Autowired
    private UserResRelationSerivce userResRelationSerivce;

    public void oper(PowerVO powerVO) {
        String auth = powerVO.getPowerStr();
        String type = "";
        Integer resourceId = powerVO.getResourceId();
        switch (powerVO.getType()) {
            case 1:
                type = "HDFS";
                break;
            case 2:
                type = "HBASE";
                break;
            case 3:
                type = "HIVE";
                break;
            case 4:
                type = "YARN";
                break;
            case 5:
                type = "KAFKA";
                break;
        }
        AuthUpdateVO authUpdate = new AuthUpdateVO();
        authUpdate.setId(resourceId);
        authUpdate.setType(type);
        List<AuthInfoVO> request = new ArrayList();
        for (Integer userId : powerVO.getTenantIdList()) {
            AuthInfoVO authInfoVO = new AuthInfoVO();
            UserResourceRelationEntity userResourceRelation = userResRelationSerivce.findByResourceIdAndUserIdAndResourceType(resourceId, userId, type);
            if (userResourceRelation != null) {
                authInfoVO.setId(userResourceRelation.getId());
            } else {
                authInfoVO.setId(null);
            }
            authInfoVO.setAuth(auth);
            authInfoVO.setUserId(userId);
            request.add(authInfoVO);
        }
        authUpdate.setAuthList(request);
        authService.updateInfo(authUpdate);
    }
}
